package com.warmheart.base.config;

import javax.annotation.PostConstruct;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * 
 * @ClassName: WhDruidConfig
 * @Description: DRUID配置
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
@Configuration
@PropertySource(factory = YamlPropertySourceFactory.class, value = "classpath:wh-druid-config.yml")
@ConfigurationProperties(prefix = "wh.druid")
public class WhDruidConfig {

    /**
     * 连接池初始连接数量
     */
    private Integer initialSize;

    /**
     * 连接池最小空闲连接数量
     */
    private Integer minIdle;

    /**
     * 连接池最大活动连接数量(注意MYSQL的最大连接数需要修改为不小于当前配置的数量)
     */
    private Integer maxActive;

    /**
     * 获取连接时最大等待时间，这里设置最大等待10秒
     */
    private Long maxWait;

    /**
     * 1、销毁线程检测连接的间隔时间，设置为60秒，如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接2、
     * testWhileIdle的判断依据
     */
    private Long timeBetweenEvictionRunsMillis;

    /**
     * 连接空闲时间大于该值，不管minIdle是多少都关闭这个连接，设置为两个小时
     */
    private Long maxEvictableIdleTimeMillis;

    /**
     * 连接空闲时间大于该值并且池中空闲连接数大于minIdle则关闭这个连接，设置为30分钟
     */
    private Long minEvictableIdleTimeMillis;

    /**
     * 检测连接是否有效SQL语句
     */
    private String validationQuery;

    /**
     * 申请连接的时候检测，如果空闲时间大于timeBetweenEvictionRunsMillis，执行validationQuery检测连接是否有效
     */
    private Boolean testWhileIdle;

    /**
     * 申请连接时执行validationQuery检测连接是否有效
     */
    private Boolean testOnBorrow;

    /**
     * 归还连接时执行validationQuery检测连接是否有效
     */
    private Boolean testOnReturn;

    /**
     * 是否缓存preparedStatement，也就是PSCache。PSCache对支持游标的数据库性能提升巨大，比如说oracle。
     * 在MYSQL下建议关闭
     */
    private Boolean poolPreparedStatements;

    /**
     * 连接泄漏检测机制
     */
    private Boolean removeAbandoned;

    /**
     * 连接泄漏检测机制参考的超时时间(单位：秒)
     */
    private Integer removeAbandonedTimeout;

    /**
     * 是否使用过滤器(这里使用WallFilter和StatFilter，前者用于防止SQL注入攻击，后者用于进行统计监控)
     */
    private String filters;

    /**
     * 连接监控属性设置
     */
    private String connectionProperties;

    /**
     * 是否使用全局监控
     */
    private Boolean useGlobalDataSourceStat;
    
    /**
     * 登录监控配置(默认127.0.0.1)
     */
    private String allowIp;

    /**
     * 登录监控名称(默认whDruid)
     */
    private String userName;

    /**
     * 登录监控密码(默认wh8520DRUID@123)
     */
    private String passWd;

    public Integer getInitialSize() {
        return initialSize;
    }

    public void setInitialSize(Integer initialSize) {
        this.initialSize = initialSize;
    }

    public Integer getMinIdle() {
        return minIdle;
    }

    public void setMinIdle(Integer minIdle) {
        this.minIdle = minIdle;
    }

    public Integer getMaxActive() {
        return maxActive;
    }

    public void setMaxActive(Integer maxActive) {
        this.maxActive = maxActive;
    }

    public Long getMaxWait() {
        return maxWait;
    }

    public void setMaxWait(Long maxWait) {
        this.maxWait = maxWait;
    }

    public Long getTimeBetweenEvictionRunsMillis() {
        return timeBetweenEvictionRunsMillis;
    }

    public void setTimeBetweenEvictionRunsMillis(Long timeBetweenEvictionRunsMillis) {
        this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
    }

    public Long getMaxEvictableIdleTimeMillis() {
        return maxEvictableIdleTimeMillis;
    }

    public void setMaxEvictableIdleTimeMillis(Long maxEvictableIdleTimeMillis) {
        this.maxEvictableIdleTimeMillis = maxEvictableIdleTimeMillis;
    }

    public Long getMinEvictableIdleTimeMillis() {
        return minEvictableIdleTimeMillis;
    }

    public void setMinEvictableIdleTimeMillis(Long minEvictableIdleTimeMillis) {
        this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
    }

    public String getValidationQuery() {
        return validationQuery;
    }

    public void setValidationQuery(String validationQuery) {
        this.validationQuery = validationQuery;
    }

    public Boolean getTestWhileIdle() {
        return testWhileIdle;
    }

    public void setTestWhileIdle(Boolean testWhileIdle) {
        this.testWhileIdle = testWhileIdle;
    }

    public Boolean getTestOnBorrow() {
        return testOnBorrow;
    }

    public void setTestOnBorrow(Boolean testOnBorrow) {
        this.testOnBorrow = testOnBorrow;
    }

    public Boolean getTestOnReturn() {
        return testOnReturn;
    }

    public void setTestOnReturn(Boolean testOnReturn) {
        this.testOnReturn = testOnReturn;
    }

    public Boolean getPoolPreparedStatements() {
        return poolPreparedStatements;
    }

    public void setPoolPreparedStatements(Boolean poolPreparedStatements) {
        this.poolPreparedStatements = poolPreparedStatements;
    }

    public Boolean getRemoveAbandoned() {
        return removeAbandoned;
    }

    public void setRemoveAbandoned(Boolean removeAbandoned) {
        this.removeAbandoned = removeAbandoned;
    }

    public Integer getRemoveAbandonedTimeout() {
        return removeAbandonedTimeout;
    }

    public void setRemoveAbandonedTimeout(Integer removeAbandonedTimeout) {
        this.removeAbandonedTimeout = removeAbandonedTimeout;
    }

    public String getFilters() {
        return filters;
    }

    public void setFilters(String filters) {
        this.filters = filters;
    }

    public String getConnectionProperties() {
        return connectionProperties;
    }

    public void setConnectionProperties(String connectionProperties) {
        this.connectionProperties = connectionProperties;
    }

    public Boolean getUseGlobalDataSourceStat() {
        return useGlobalDataSourceStat;
    }

    public void setUseGlobalDataSourceStat(Boolean useGlobalDataSourceStat) {
        this.useGlobalDataSourceStat = useGlobalDataSourceStat;
    }
    
    public String getAllowIp() {
        return allowIp;
    }

    public void setAllowIp(String allowIp) {
        this.allowIp = allowIp;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassWd() {
        return passWd;
    }

    public void setPassWd(String passWd) {
        this.passWd = passWd;
    }

    /**
     * 
     * @Title: setProperties
     * @Description: 消除销毁数据库连接报警信息
     * @author ZhangJianyang
     */
    @PostConstruct
    public void setProperties() {
        System.setProperty("druid.mysql.usePingMethod", "false");
    }
}
